package com.truonghau.shipper.database;
import java.util.ArrayList;
import java.util.List;

import com.truonghau.shipper.providers.helper.ListMocDatabaseHelper;
import com.truonghau.model.ListMocDTO;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class ListMocDatabase {
	// Database fields
	private SQLiteDatabase database;
	private ListMocDatabaseHelper dbHelper;
	private String[] allColumns = { ListMocDatabaseHelper.COLUMN_ID,
			ListMocDatabaseHelper.COLUMN_FILENAME, 
			ListMocDatabaseHelper.COLUMN_ISDEFAULT,
			ListMocDatabaseHelper.COLUMN_TITLE};

	public ListMocDatabase(Context context) {
		dbHelper = new ListMocDatabaseHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public ListMocDTO createRecord(ListMocDTO listmoc) {
		ContentValues values = new ContentValues();
		values.put(ListMocDatabaseHelper.COLUMN_FILENAME, listmoc.getFileName());
		values.put(ListMocDatabaseHelper.COLUMN_ISDEFAULT, listmoc.getIsDefault());
		values.put(ListMocDatabaseHelper.COLUMN_TITLE, listmoc.getTitle());
		long insertId = database.insert(ListMocDatabaseHelper.TABLE_NAME, null,
				values);
		Cursor cursor = database.query(ListMocDatabaseHelper.TABLE_NAME,
				allColumns, ListMocDatabaseHelper.COLUMN_ID + " = " + insertId, null,
				null, null, null);
		cursor.moveToFirst();
		ListMocDTO newRecord = cursorToObject(cursor);
		cursor.close();
		return newRecord;
	}
	
	public void changeDefault(ListMocDTO listmoc){
		String fileName = getDefaultFileName();
		
		ContentValues args = new ContentValues();
		args.put(ListMocDatabaseHelper.COLUMN_ISDEFAULT, false);
		database.update(ListMocDatabaseHelper.TABLE_NAME, args, null , null);
		// set new default
		if(listmoc != null && !listmoc.getFileName().equals(fileName)){
			ContentValues argsNew  = new ContentValues();
			argsNew.put(ListMocDatabaseHelper.COLUMN_ISDEFAULT, true);
			long id = listmoc.getId();
			database.update(ListMocDatabaseHelper.TABLE_NAME, argsNew, ListMocDatabaseHelper.COLUMN_ID + " = " + id, null);
		}
	}
	public void updateTitle(ListMocDTO listmoc){
		ContentValues args = new ContentValues();
		args.put(ListMocDatabaseHelper.COLUMN_TITLE, listmoc.getTitle());
		long id = listmoc.getId();
		database.update(ListMocDatabaseHelper.TABLE_NAME, args, ListMocDatabaseHelper.COLUMN_ID + " = " + id , null);
	}
	public void deleteRecord(ListMocDTO listmoc) {
		long id = listmoc.getId();
		System.out.println("Comment deleted with id: " + id);
		database.delete(ListMocDatabaseHelper.TABLE_NAME, ListMocDatabaseHelper.COLUMN_ID
				+ " = " + id, null);
	}

	public List<ListMocDTO> getAllRecords() {
		List<ListMocDTO> listmocs = new ArrayList<ListMocDTO>();

		Cursor cursor = database.query(ListMocDatabaseHelper.TABLE_NAME,
				allColumns, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			ListMocDTO listmoc = cursorToObject(cursor);
			listmocs.add(listmoc);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return listmocs;
	}

	private ListMocDTO cursorToObject(Cursor cursor) {
		ListMocDTO listmoc = new ListMocDTO();
		listmoc.setId(cursor.getLong(0));
		listmoc.setFileName(cursor.getString(1));
		listmoc.setTitle(cursor.getString(3));
		listmoc.setIsDefault("1".equals(cursor.getString(2))?true:false);
		return listmoc;
	}
	public String getDefaultFileName(){
		String sql =ListMocDatabaseHelper.COLUMN_ISDEFAULT + " = " + "1";
		Cursor cursor = database.query(ListMocDatabaseHelper.TABLE_NAME,
				allColumns, sql, null, null, null, null);
		if(cursor == null || cursor.getCount() == 0) return null;
		
		cursor.moveToFirst();
		ListMocDTO listmoc = cursorToObject(cursor);
		// Make sure to close the cursor
		cursor.close();
		return listmoc.getFileName();
	}
}
